package com.cqs.leetcode.queueStack;

import java.util.Stack;

/**
 * @author lixiaowen
 * @create 2019-12-13
 */
public class ImplementQueueusingStacks232 {

private Stack<Integer> stack = new Stack<>();
private Stack<Integer> helper = new Stack<>();

/** Push element x to the back of queue. */
public void push(int x) {
    stack.push(x);
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
    if (helper.isEmpty()){
        while (!stack.isEmpty()){
            helper.push(stack.pop());
        }
    }
    return helper.pop();
}

/** Get the front element. */
public int peek() {
    if (helper.isEmpty()){
        while (!stack.isEmpty()){
            helper.push(stack.pop());
        }
    }
    return helper.peek();
}

/** Returns whether the queue is empty. */
public boolean empty() {
    return helper.isEmpty() && stack.isEmpty();
}

    public static void main(String[] args) {
        ImplementQueueusingStacks232 queue = new ImplementQueueusingStacks232();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        System.out.println(queue);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        queue.push(5);
        System.out.println(queue.pop());
    }

    @Override
    public String toString() {
        return "ImplementQueueusingStacks232{" +
                "stack=" + stack +
                '}';
    }
}
